package com.ymt.bpm.web.rest.platform.config;

import com.alibaba.fastjson.JSONObject;
import com.ymt.bpm.service.platform.config.BizDataManagerService;
import com.ymt.bpm.util.AES;
import com.ymt.bpm.util.Const;
import com.ymt.bpm.util.EncodeUtil;
import com.ymt.fmd.model.PBdm;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/api/v1/platform/config/bdm")
public class BDMResource {

    /**
     * Logger
     */
    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private BizDataManagerService bdms;

    @GetMapping("/query")
    public JSONObject query(HttpServletRequest req,
                            @RequestParam(name="appId", required = false)String appId,
                            @RequestParam(name="search", required = false)String search,
                            @RequestParam(name="sort", required = false)String sort,
                            @RequestParam(name="order", required = false)String order,
                            @RequestParam("offset")int offset,
                            @RequestParam("limit")int limit,
                            @RequestParam("timezoneOffset")int timezoneOffset) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        if (search!=null && search.length()>0) {
            search = EncodeUtil.decode(search);
        }
        if (log.isDebugEnabled()) {
            log.debug("query tenantId=" + tenantId);
            log.debug("query search=" + search);
            log.debug("query sort=" + sort);
            log.debug("query order=" + order);
            log.debug("query offset=" + offset);
            log.debug("query limit=" + limit);
            log.debug("query timezoneOffset=" + timezoneOffset);
        }
        return bdms.query(tenantId, appId, search, sort, order, offset, limit, timezoneOffset);
    }

    @GetMapping("/{bdmId}")
    public JSONObject get(HttpServletRequest req,
                          @RequestParam(name="enc", required = false)boolean enc,
                          @RequestParam(name="appId", required = false)String appId,
                          @PathVariable(name="bdmId")String bdmId,
                          @RequestParam(name="timezoneOffset")int timezoneOffset) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        String lang = userDetail.get(Const.TENANT_ID);
        if (enc) {
            bdmId = AES.decrypt(bdmId);
        }
        if (log.isDebugEnabled()) {
            log.debug("get tenantId=" + tenantId);
            log.debug("get enc=" + enc);
            log.debug("get bdmId=" + bdmId);
            log.debug("get lang=" + lang);
            log.debug("get timezoneOffset=" + timezoneOffset);
        }
        return bdms.get(tenantId, appId, bdmId, lang, timezoneOffset);
    }

    @PostMapping("/save")
    public JSONObject save(HttpServletRequest req, PBdm bdm) {
        if (log.isDebugEnabled()) {
            log.debug("save bdmId=" + bdm.getBdmId());
        }
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String loginName = userDetail.get(Const.LOGINNAME);
        String displayName = userDetail.get(Const.DISPLAYNAME);
        String tenantId = userDetail.get(Const.TENANT_ID);
        return bdms.save(tenantId, bdm, loginName, displayName);
    }

    @PostMapping(value="/checkName", produces = "plain/text")
    public String checkBoname(HttpServletRequest req,
                              @RequestParam(name="appId", required = false)String appId,
                              @RequestParam(value="bdmId", required=false)String bdmId,
                              @RequestParam("name")String name) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        if (log.isDebugEnabled()) {
            log.debug("checkBoname tenantId=" + tenantId);
            log.debug("checkBoname name=" + name);
            log.debug("checkBoname bdmId=" + bdmId);
        }
        return bdms.checkAttr(tenantId, appId,"name", name, bdmId);
    }

    @DeleteMapping("/delete/{bdmId}")
    public JSONObject delete(HttpServletRequest req,
                             @RequestParam(name="appId", required = false)String appId,
                             @PathVariable(name="bdmId")String bdmId) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        String loginName = userDetail.get(Const.LOGINNAME);
        String displayName = userDetail.get(Const.DISPLAYNAME);
        if (log.isDebugEnabled()) {
            log.debug("delete tenantId=" + tenantId);
        }
        return bdms.delete(tenantId, loginName, displayName, appId, bdmId);
    }
    
}
